<?php

namespace App\Admin\Controllers;

use App\Admin\AdminUpload;
use App\Admin\Grid\NGrid;
use App\Admin\Service\UploadFileService;
use App\Library\Logic\OperateLogLogic;
use App\Models\UploadFileModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Show;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;

class UploadFileController extends AdminController
{
    private UploadFileService $service;

    /**
     * @param UploadFileService $service
     */
    public function __construct(UploadFileService $service)
    {
        $this->service = $service;
    }

    /**
     * grid
     * @return NGrid
     */
    protected function grid(): NGrid
    {
        $this->service->grid();
        $this->service->column('serialNumber,userId,imgWidth,imgHeight,createdAt');
        $this->service->using('mimeType');
        $this->service->grid->column('file_size', $this->service->ml['fileSize'])->display(function ($column) {
            if ($column >= 1048576) return bcdiv($column, 1048576, 2) . 'M';
            else if ($column >= 1024) return bcdiv($column, 1024, 2) . 'K';
            else return $column . 'B';
        });
        $this->service->grid->disableCreateButton(false);
        $this->service->grid->disableActions(false);
        $this->service->grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableView(false);
            $action->disableEdit();
        });
        return $this->service->grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $this->service->show($id);
        $this->service->field('serialNumber,userId,imgWidth,imgHeight,fileExt,encoding,playtimeSeconds,bitrate,playtimeString,url,createdAt');
        $this->service->using('mimeType');
        $this->service->show->field('file_size', $this->service->ml['fileSize'])->unescape()->as(function ($column) {
            if ($column >= 1048576) return bcdiv($column, 1048576, 2) . 'M';
            else if ($column >= 1024) return bcdiv($column, 1024, 2) . 'K';
            else return $column . 'B';
        });
        if (in_array($this->service->show->getModel()->mime_type, __('mime_type.img'))) {
            $this->service->show->field('fileId', '图片')->unescape()->as(function () {
                return view('image', ['size' => 100, 'image' => $this->url]);
            });
        } else if (in_array($this->service->show->getModel()->mime_type, __('mime_type.video'))) {
            $this->service->show->field('video', '视频')->unescape()->as(function () {
                return view('video', ['url' => $this->url]);
            });
        } else if (in_array($this->service->show->getModel()->mime_type, __('mime_type.audio'))) {
            $this->service->show->field('audio', '音频')->unescape()->as(function () {
                return view('audio', ['url' => $this->url]);
            });
        }
        //$this->service->show->field('oss_info', 'info')->json();
        $this->service->field('ossInfo')->unescape()->as(function ($column) {
            return view('json', ['json' => $column]);
        });
        return $this->service->show;
    }

    public function form(): Form
    {
        $form = new Form(new UploadFileModel);
        $form->image('file', '文件')->required();
        $form->setAction('/admin/uploadFile/save');
        OperateLogLogic::add(Admin::user()->user_id);
        return $form;
    }

    /**
     * save
     * @return Application|Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function save(): Application|Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
    {
        AdminUpload::upload(request()->file('file'));
        return redirect('/admin/uploadFile');
    }
}
